package org.jsola.contract.service.impl;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dao.ICategoryVariableDAO;
import org.jsola.contract.dto.CategoryVariableAddDTO;
import org.jsola.contract.dto.CategoryVariableUpdateDTO;
import org.jsola.contract.entity.CategoryVariableDO;
import org.jsola.contract.query.CategoryVariableQuery;
import org.jsola.contract.service.ICategoryVariableService;
import org.jsola.contract.vo.CategoryVariableListVO;
import org.jsola.contract.vo.CategoryVariableVO;
import org.jsola.core.Page;
import org.jsola.orm.filter.Order;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 合同分类下模板变量
 *
 * @author JanusMix
 */
@Service("contractCategoryVariableServiceImpl")
public class CategoryVariableServiceImpl implements ICategoryVariableService {

    @Autowired
    private ICategoryVariableDAO categoryVariableDAO;

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public CategoryVariableVO save(CategoryVariableAddDTO categoryVariableAddDTO, TokenUser tokenUser) {
        // 转DO
        CategoryVariableDO categoryVariableDO = categoryVariableAddDTO.to(CategoryVariableDO.class);
        // 保存
        categoryVariableDO = save(categoryVariableDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return categoryVariableDO.to(CategoryVariableVO.class);
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int update(CategoryVariableUpdateDTO categoryVariableUpdateDTO, TokenUser tokenUser) {
        //转DO
        CategoryVariableDO categoryVariableDO = categoryVariableUpdateDTO.to(CategoryVariableDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(categoryVariableDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... categoryVariableIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) categoryVariableIds);
    }


    @Override
    public CategoryVariableVO selectById(Long categoryVariableId, String siteId) {
        CategoryVariableDO categoryVariableDO = selectDOById(categoryVariableId, siteId);
        if (categoryVariableDO == null) {
            return null;
        }
        return categoryVariableDO.to(CategoryVariableVO.class);
    }

    @Override
    public List<CategoryVariableListVO> select(CategoryVariableQuery categoryVariableQuery, String siteId) {
        List<CategoryVariableDO> categoryVariableDOList = selectDO(categoryVariableQuery, siteId);
        if(CollectionUtils.isEmpty(categoryVariableDOList)) {
            return categoryVariableDOList == null ? null : new ArrayList<>();
        }
        return categoryVariableDOList.stream()
                .map(categoryVariableDO -> categoryVariableDO.to(CategoryVariableListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CategoryVariableQuery categoryVariableQuery, String siteId) {
        return categoryVariableDAO.findCountByQuery(categoryVariableQuery, siteId);
    }

    @Override
    public Page<CategoryVariableListVO> selectPage(CategoryVariableQuery categoryVariableQuery, String siteId) {
        //将order参数中的propertyName改为dbName
        List<Order> orders = categoryVariableQuery.getOrders();
        if (!CollectionUtils.isEmpty(orders)) {
            orders.forEach(order ->
                order.setProperty(EntityHelper.getEntityTable(CategoryVariableDO.class).getPropertyMap().get(order.getProperty()).getColumn())
            );
        }
        categoryVariableQuery.setOrders(orders);
        Page<CategoryVariableDO> page = categoryVariableDAO.findPageByQuery(categoryVariableQuery, siteId);
        return page.to(CategoryVariableListVO.class);
    }

    @Override
    public CategoryVariableDO selectDOById(Long categoryVariableId, String siteId) {
        return categoryVariableDAO.findById(categoryVariableId, siteId);
    }

    @Override
    public List<CategoryVariableDO> selectDO(CategoryVariableQuery categoryVariableQuery, String siteId) {
        //将order参数中的propertyName改为dbName
        List<Order> orders = categoryVariableQuery.getOrders();
        if (!CollectionUtils.isEmpty(orders)) {
            orders.forEach(order ->
                order.setProperty(EntityHelper.getEntityTable(CategoryVariableDO.class).getPropertyMap().get(order.getProperty()).getColumn())
            );
        }
        categoryVariableQuery.setOrders(orders);
        return categoryVariableDAO.findListByQuery(categoryVariableQuery, siteId);
    }


}




